package com.huatai.datacenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.common.util.StringUtil;
import com.huatai.datacenter.entity.dataquality.QualityBaseEntity;
import com.huatai.datacenter.entity.dataquality.QualityLogDetailEntity;
import com.huatai.datacenter.entity.dataquality.QualityLogEntity;
import com.huatai.datacenter.mapper.QualityLogMapper;
import com.huatai.datacenter.service.QualityLogDetailService;
import com.huatai.datacenter.service.QualityLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gusiyi
 * @version 1.0
 * @className QualityLogServiceImpl
 * @description 描述：
 * @date 2023/6/5 15:08
 */
@Service
public class QualityLogServiceImpl extends ServiceImpl<QualityLogMapper,QualityLogEntity> implements QualityLogService {

	@Autowired
	private QualityLogDetailService detailService;

	@Override
	public Page<QualityLogEntity> getQualityLogList(QualityLogEntity qualityLogEntity) {
		LambdaQueryWrapper<QualityLogEntity> logQuery = Wrappers.lambdaQuery();
		logQuery
			.eq(QualityLogEntity::getDeleted,false)
			.like(StringUtil.isNotBlank(qualityLogEntity.getDataModelName()),QualityLogEntity::getDataModelName, qualityLogEntity.getDataModelName())
			.orderByDesc(QualityBaseEntity::getUpdateTime);
		Page<QualityLogEntity> page = new Page<>(qualityLogEntity.getPageIndex(),qualityLogEntity.getPageSize());
		Page<QualityLogEntity> qualityLogEntityPage = page(page, logQuery);
		for (QualityLogEntity qualityLogData : qualityLogEntityPage.getRecords()) {
			//查询错误日志总数
			LambdaQueryWrapper<QualityLogDetailEntity> logDetailCountQuery = Wrappers.lambdaQuery();
			logDetailCountQuery
				.eq(QualityLogDetailEntity::getDeleted,false)
				.eq(QualityLogDetailEntity::getQualityLogId,qualityLogData.getId());
			Long errCount = detailService.count(logDetailCountQuery);
			qualityLogData.setErrLogCount(errCount);
			//查询上次错误日志总数
			LambdaQueryWrapper<QualityLogDetailEntity> logDetailLastCountQuery = Wrappers.lambdaQuery();
			logDetailLastCountQuery
				.eq(QualityLogDetailEntity::getDeleted,false)
				.eq(QualityLogDetailEntity::getQualityLogId,qualityLogData.getId())
				.eq(QualityLogDetailEntity::getExecuteTime,qualityLogData.getLastExecuteTime());
			Long lastErrLogCount = detailService.count(logDetailLastCountQuery);
			qualityLogData.setLastCheckErrLogCount(lastErrLogCount);
		}
		return qualityLogEntityPage;
	}

	@Override
	@Transactional(rollbackFor = {Exception.class})
	public boolean deleteByIds(List<QualityLogEntity> qualityLogEntityList) {
		List<Long> deleteLogDetailList = new ArrayList<>(qualityLogEntityList.size());
		for (QualityLogEntity qualityLogEntity : qualityLogEntityList) {
			deleteLogDetailList.add(qualityLogEntity.getId());
			qualityLogEntity.setDeleted(true);
		}
		QualityLogDetailEntity logDetailEntity = new QualityLogDetailEntity();
		logDetailEntity.setDeleted(true);
		LambdaQueryWrapper<QualityLogDetailEntity> logDetailQuery = Wrappers.lambdaQuery();
		logDetailQuery
			.eq(QualityLogDetailEntity::getDeleted,false)
			.in(QualityLogDetailEntity::getQualityLogId,deleteLogDetailList);
		detailService.update(logDetailEntity,logDetailQuery);
		return updateBatchById(qualityLogEntityList);
	}

	@Override
	@Transactional(rollbackFor = {Exception.class})
	public boolean addErrLogDetail(QualityLogDetailEntity qualityLogDetailEntity) {
		detailService.save(qualityLogDetailEntity);
		QualityLogEntity logEntity = new QualityLogEntity();
		logEntity.setId(qualityLogDetailEntity.getQualityLogId());
		logEntity.setLastExecuteTime(qualityLogDetailEntity.getExecuteTime());
		logEntity.setNextExecuteTime(qualityLogDetailEntity.getNextExecuteTime());
		return updateById(logEntity);
	}

	@Override
	public QualityLogEntity getLogEntity(Long dataModelId, String dataModelName) {
		LambdaQueryWrapper<QualityLogEntity> logQuery = Wrappers.lambdaQuery();
		logQuery
			.eq(QualityBaseEntity::getDeleted,false)
			.eq(QualityLogEntity::getDataModelId,dataModelId);
		QualityLogEntity logEntity = getOne(logQuery);
		if (logEntity == null){
			logEntity = new QualityLogEntity();
			logEntity.setDataModelId(dataModelId);
			logEntity.setDataModelName(dataModelName);
			save(logEntity);
		}
		return logEntity;
	}

}
